@extends('admin.layout.global')

@section('css-plugins')
    <link href="{{ asset('style/plugins/toastr/toastr.min.css') }}" rel="stylesheet">
    <link href="{{ asset('style/plugins/iCheck/custom.css') }}" rel="stylesheet">
@endsection

@section('css')
    <link href="{{ asset('style/admin/css/custom.css') }}" rel="stylesheet">
    <style>
        .pagination {
            margin: 0 0;
        }
    </style>
@endsection

@section('js-plugins')
    <script src="{{ asset('style/plugins/toastr/toastr.min.js') }}"></script>
    <script src="{{ asset('style/plugins/iCheck/icheck.min.js') }}"></script>
@endsection

@section('js')
    <script>
        $(function(){
            toastr.options = {
                "progressBar": true,
                "positionClass": "toast-top-right",
                "timeOut": "1200",
            }
            $('input[type="checkbox"]').iCheck({
                checkboxClass: 'icheckbox_square-green checkthisis'
            });

            $(".iCheck-helper",".checkthisis").click(function ()
            {
                //var obj = this;
                var chk = $(".iCheck-helper",".checkthisis");
                var count = chk.length;
                var num = chk.index(this);
                var chkinput = $("input[type='checkbox']",".checkthisis");

                var level_top = level_bottom =  chkinput.eq(num).attr('level')
                for (var i=num; i>=0; i--)
                {
                    var le = chkinput.eq(i).attr('level');
                    if(eval(le) < eval(level_top))
                    {
                        chkinput.eq(i).iCheck("check");
                        var level_top = level_top-1;
                    }
                }
                for (var j=num+1; j<count; j++)
                {
                    var le = chkinput.eq(j).attr('level');
                    if(chkinput.eq(num).prop("checked")) {
                        if(eval(le) > eval(level_bottom))
                            chkinput.eq(j).iCheck("check");
                        else if(eval(le) == eval(level_bottom))
                            break;
                    }
                    else {
                        if(eval(le) > eval(level_bottom))
                            chkinput.eq(j).iCheck("uncheck");
                        else if(eval(le) == eval(level_bottom))
                            break;
                    }
                }
            });

            $("[type='checkbox']").on('switchChange.bootstrapSwitch', function(event, state) {
                var id = $(this).data('id');
                var display = 0;
                if(state)
                {
                    display = 1;
                }
                $.ajax({
                    url: "{{ action('Admin\MenuController@postDisplay') }}",
                    data: { id:id,display:display,_token:'{{ csrf_token() }}'},
                    type:'post',
                    success: function (data) {
                        var jsonData = JSON.parse(data);
                        if (jsonData.code)
                        {
                            toastr.success(jsonData.message);
                        } else {
                            toastr.error(jsonData.message);
                        }
                    }
                })
            });
        });
    </script>
@endsection

@section('content')
    <div class="body-content animated fadeIn">
        <div class="row">
            <div class="col-md-4">

                <!-- Start basic color table -->
                <div class="panel">
                    <div class="panel-body no-padding">
                        <form method="post">
                            {!! csrf_field() !!}
                            <div class="table-responsive" style="margin-top: -1px;">
                                <table class="table table-striped table-success">
                                    <thead>
                                    <tr>
                                        <th class="border-right" >{{ getLang('fieldsAdminGroup', 'auth') }}</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    {!! $tree !!}
                                    </tbody>
                                </table>
                            </div><!-- /.table-responsive -->
                            <div class="row">
                                <div class="col-md-6">
                                    <a href="{{ action('Admin\AdminGroupController@getIndex') }}" class="btn btn-warning btn-sm btn-block"><i class="fa fa-reply"></i> <span class="bold">{{ getLang('Tips', 'rollBack') }}</span></a>
                                </div>
                                <div class="col-md-6">
                                    <button class="btn btn-primary btn-sm btn-block" type="submit"><i class="fa fa-check"></i>&nbsp;{{ getLang('Tips', 'submit') }}</button>
                                </div>
                            </div>
                        </form>
                    </div><!-- /.panel-body -->
                </div><!-- /.panel -->
                <!--/ End basic color table -->

            </div><!-- /.col-md-12 -->
        </div>
    </div>
@endsection